<template>
  <div id="shopLineChart">
    <div ref="chart01" class="chart_box"></div>
  </div>
</template>

<script>
export default {
  name: 'liveLine',
  components: {},
  props: {
    xData: Array | Object,
    series1: Array | Object,
    series2: Array | Object,
    name1: String,
    name2: String,
    nasns: true,
    nasns1: true
  },
  data() {
    return {
      name_1: ''
      // xZ:["1623686400","1623772800","1623859200","1623945600","1624032000","1624118400","1624204800","1624291200","1624377600","1624464000","1624550400","1624636800","1624723200","1624809600","1624896000"],
      // xD:[],
      // s1:["5388","18552","4446","40381","13983","5000","12310","24350","16007","28480","71094","96723","106078","69774","61623"],
      // s2:["3079","7412","1214","16353","5254","2363","6214","12002","7552","13558","38585","60099","56558","33125","21857"],
    };
  },
  computed: {},
  watch: {
    xData: {
      handler(n, o) {
        this.drawLiveLine();
      },
      deep: true
    },
    series1: {
      handler(n, o) {
        this.drawLiveLine();
      },
      deep: true
    },
    series2: {
      handler(n, o) {
        this.drawLiveLine();
      },
      deep: true
    },
    name1(n) {
      this.drawLiveLine();
    },
    name2(n) {
      if (n) {
        this.name_1 = n;
      } else {
        this.name_1 = '';
      }
      this.drawLiveLine();
    }
  },

  created() {
    // this.xD = this.xZ.map((element) => {
    //   return this.timestamp(element, "M/D");
    // });
  },
  mounted() {
    if (this.xData) {
      this.$nextTick(() => {
        this.drawLiveLine();
      });
    }
  },

  methods: {
    drawLiveLine() {
      let that = this;
      let mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'line', // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: '#e5e5e5'
            }
          },
          formatter: function (params) {
            let result01 = '';
            params.forEach(function (item) {
              if (item.seriesName == '好评率' || item.seriesName == '差评率') {
                if (that.format_num(item.value) == 0) {
                  result01 +=
                    item.marker +
                    ' ' +
                    item.seriesName +
                    ' : ' +
                    that.format_num(item.value) +
                    '</br>';
                } else {
                  result01 +=
                    item.marker + ' ' + item.seriesName + ' : ' + item.value * 100 + '%' + '</br>';
                }
              } else {
                result01 +=
                  item.marker +
                  ' ' +
                  item.seriesName +
                  ' : ' +
                  that.format_num(item.value) +
                  '</br>';
              }
            });
            return params[0].name + '</br>' + result01;
          }
        },
        legend: {
          icon: 'rect ',
          top: 28,
          itemWidth: 20,
          itemHeight: 3,
          itemGap: 30,
          textStyle: {
            color: '#555'
          }
        },
        grid: {
          left: '58',
          right: '58',
          bottom: '68',
          containLabel: false
        },
        xAxis: [
          {
            type: 'category',
            data: this.xData,
            boundaryGap: false, //X轴起始位置靠着Y轴
            //设置网格线颜色
            splitLine: {
              show: false,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            // x轴样式
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            },
            axisTick: {
              show: false
            },
            // x轴字体样式
            axisLabel: {
              // interval: 0,
              // rotate: 40,
              margin: 14,
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: this.name1,
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, 45]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                if (!that.nasns1) {
                  return that.format_num(value);
                } else {
                  return that.format_num(value) * 100 + '%';
                }
              }
              // formatter: function(value) {
              //   console.log(value);
              //   return that.format_num(value);
              // }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          },
          {
            type: 'value',
            name: this.name_1,
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, -55]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                if (!that.nasns) {
                  return that.format_num(value);
                } else {
                  return that.format_num(value) * 100 + '%';
                }
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          }
        ],
        dataZoom: [
          {
            type: 'slider',
            realtime: true,
            start: 0,
            end: 100,
            // backgroundColor: "#f0935e",
            fillerColor: '#f0935e',
            height: '20'
            // border: "0",
            // borderColor: "#f0935e"
            // borderColor:"#ddd",
          }
        ],
        series: [
          // {
          //   symbol:`image://${testIcon}`,
          //   symbolSize:'20',
          //   type:'scatter',
          //   data:[0,0,0,0,0,0,0,0]
          // },
          {
            name: this.name1,
            type: 'line',
            smooth: true, //true 为平滑曲线，false为直线
            symbol: 'none', //去掉折线上的小圆点
            data: this.series1,
            yAxisIndex: 0,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  //渐变色实现===
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: '#FED1B4'
                      },
                      {
                        offset: 1,
                        color: '#FAFAFB'
                      }
                    ]
                  )
                },
                color: '#fd7f2c'
                //柱子颜色
              }
            }
          },
          {
            name: this.name_1,
            type: 'line',
            smooth: true, //true 为平滑曲线，false为直线
            symbol: 'none', //去掉折线上的小圆点
            yAxisIndex: 1,
            data: this.series2,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  //渐变色实现===
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: '#ACCFFF'
                      },
                      {
                        offset: 1,
                        color: '#FAFAFB'
                      }
                    ]
                  )
                },
                color: '#2781FF'
                //柱子颜色
              }
            }
          }
        ]
      };
      mychart.setOption(option);
      //   窗口自适应
      setTimeout(() => {
        let resizeFn = function () {
          mychart.resize();
        };
        window.addEventListener('resize', resizeFn);
        this.$once('hook:beforeDestroy', () => {
          mychart.dispose();
          mychart = null;
          window.removeEventListener('resize', resizeFn);
        });
      }, 200);
    }
  }
};
</script>
<style lang="less" scoped>
#shopLineChart {
  width: 100%;
  height: 100%;
  // margin-top: 36px;
  .chart_box {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
}
</style>
